package com.gavin.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

	/**yyyy-MM-dd HH:mm:ss:SSS**/
	public final static String FORMAT_YMDHMSS = "yyyy-MM-dd HH:mm:ss:SSS";
	/**yyyy-MM-dd HH:mm:ss**/
	public final static String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	/**yyyy-MM-dd**/
	public final static String FORMAT_YMD = "yyyy-MM-dd";

	public static long string2Time(String strDate, String format) {
		Date date = string2Date(strDate, format);
		if (date == null) {
			return -1;
		} else {
			return date.getTime();
		}
	}

	public static long string2Time(String strDate) {
		return string2Time(strDate, FORMAT_YMDHMS);
	}

	public static Date string2Date(String strDate) {
		return string2Date(strDate, FORMAT_YMDHMS);
	}

	public static Date string2Date(String strDate, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String date2String(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String date2String(Date date) {
		return date2String(date, FORMAT_YMDHMS);
	}

	public static String time2String(long lTime, String format) {
		Date date = new Date(lTime);
		return date2String(date, format);
	}

	public static String time2String(long lTime) {
		return time2String(lTime, FORMAT_YMDHMS);
	}

	public static String getNow(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static String getNow() {
		return getNow(FORMAT_YMDHMS);
	}
	
	// time util start--------------------------------------------------------------------------------------------------------------------
	public static String timestamp2String(String format) {
		if (format == null || format.length() == 0) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		return new SimpleDateFormat(format).format(new Timestamp(System.currentTimeMillis()));
	}

	public static String timestamp2String(long now, String format) {
		if (format == null || format.length() == 0) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		return new SimpleDateFormat(format).format(new Timestamp(now));
	}

	public static String timestamp2String(Timestamp timestamp, String format) {
		if (format == null || format.length() == 0) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		return new SimpleDateFormat(format).format(timestamp);
	}
	// time util end--------------------------------------------------------------------------------------------------------------------

}
